<template>
  <el-button
    :size="size"
    type="primary"
    icon="el-icon-download"
    circle
    @click="download()"
  ></el-button>
</template>

<script>
import { download } from '@/api/resource'
export default {
  name: 'DownloadButton',
  props: {
    resourceID: Number,
    resource: Object,
    size: String
  },
  computed: {
    url() {
      return `/${this.resource.period}/${this.resource.subject}/${this.resource.resourceType}/${this.resource.id}.${this.resource.fileType}`
    },
    filename() {
      return this.resource.filename + '.' + this.resource.fileType
    }
  },
  methods: {
    download() {
      const { user } = this.$store.state
      if (!user) {
        this.$message.warning('请先登录')
        this.$router.push({
          path: '/login',
          query: { redirect: this.$route.fullPath }
        })
      } else {
        download(this.resourceID).then((response) => {
          if (!response) return
          window.open(response.config.baseURL + response.config.url)
        })
        // minioClient.presignedGetObject('madfile', this.url, 24 * 60 * 60, function(err, presignedUrl) {
        //   if (err) return console.log(err)
        //   const element = document.createElement('a')
        //   element.href = presignedUrl
        //   element.style.display = 'none'
        //   element.click()
        //   // 在资源下载完成后 清除 占用的缓存资源
        //   window.URL.revokeObjectURL(element.href)
        //   document.body.removeChild(element)
        // })
      }
      // if (user.userType < 1) {
      //
      //   return
      // }
      // console.log(user.userType)
      // download(this.resourceID)
    }
  }
}
</script>

<style scoped></style>
